package com.xiaoyi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avin.yi.R;
import com.xiaoyi.listener.OnMusicItemClickListener;
import com.xiaoyi.tool.AvBaseTool;

import java.util.List;


public class ListTxtItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<String> datas;
    private OnMusicItemClickListener onItemClickListener;

    public ListTxtItemAdapter(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setOnItemClickListener(OnMusicItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_onetxt, parent, false);
        MyHolder holder = new MyHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ((MyHolder) holder).title.setText(datas.get(position) );
        ((MyHolder) holder).rlRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(position);
            }
        });


        if (selectedPosition == position) {
            ((MyHolder) holder).rlRoot.setBackgroundResource( R.drawable.item_sharpback_press);
        } else {
            ((MyHolder) holder).rlRoot.setBackgroundResource(R.drawable.item_sharpback);
        }

    }


    private int selectedPosition =0 ;
    public void setSelectedItemColor( int selectedPosition){
        this.selectedPosition = selectedPosition ;
        notifyItemChanged(selectedPosition);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private RelativeLayout rlRoot;

        public MyHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            title.setTextColor(Color.BLACK);
            rlRoot = itemView.findViewById(R.id.rl_root);
        }
    }


}
